<template>
  <ContextMenu
    :menu="[
      { label: '置顶', data: chatUser, handle: noChat },
      { label: '标为未读', data: chatUser, handle: noChat },
      { label: '消息免打扰', data: chatUser, handle: noChat },
      { label: '在独立窗口中打开', data: chatUser, handle: noChat },
      { label: '不显示聊天', data: chatUser, handle: removeChat },
      { label: '删除聊天', data: chatUser, handle: deleteChat },
    ]"
    @select="$event.handle($event)"
  >
    <div class="user-item" :class="{ active: isActive }" @click="clickUser">
      <div class="item-left">
        <el-badge
          :badge-class="'avatar-box'"
          :hidden="!chatUser.hasNewMessage"
          :value="chatUser.newMessageCount"
          :max="99"
        >
          <el-avatar :shape="'square'" :src="chatUser.toUserAvatar ?? circleUrl" />
        </el-badge>
      </div>
      <div class="item-right">
        <div class="right-top">
          <div class="top-name">
            {{ chatUser.toNickName }}
          </div>
          <div class="top-time">
            {{ lastTime }}
          </div>
        </div>
        <div class="right-bottom" v-if="isImage">
          <el-image
            class="image-content"
            :shape="'square'"
            :size="20"
            :src="chatUser.lastMessage"
          />
        </div>
        <div class="right-bottom" v-else>
          {{ chatUser.lastMessage }}
        </div>
      </div>
    </div>
  </ContextMenu>
</template>

<script lang="ts" setup>
import { dayjs } from "element-plus";
import { emit } from "@/utils/mitt";
import ChatUserApi from "@/api/chat/chatUser";
import FileApi from "@/api/file";
import { isUrl } from "@/utils/urlUtils";
import { useUserStore } from "@/store";

const props = defineProps({
  chatUser: Object, // as PropType<ChatUserModel>,
  isActive: Boolean,
});

const userStore = useUserStore();

const lastTime = computed(() => {
  return dayjs(props.chatUser.lastTime).format("HH:mm");
});

const isImage = computed(() => {
  return isUrl(props.chatUser.lastMessage) ? true : false;
});

const circleUrl = FileApi.GetDefaultUserAvatar();

const emits = defineEmits(["active", "remove"]);

const clickUser = () => {
  emit("chat:active:user", props.chatUser);
  emits("active", props.chatUser);
};

const removeChat = async (item) => {
  const { success, message } = await ChatUserApi.hide(item.data.id);
  if (success) {
    userStore.clearMessage();
    emits("remove");
  } else {
    ElNotification({
      title: message,
      type: "error",
    });
  }
};
const deleteChat = async (item) => {
  // ElNotification.info("该功能暂未实现！")
  //DODO：消息是同一条数据，一遍删掉另一遍也会删掉，需要改表结构
  const { success, message } = await ChatUserApi.delete(item.data.id);
  if (success) {
    userStore.clearMessage();
    emits("remove");
  } else {
    ElNotification({
      title: message,
      type: "error",
    });
  }
};

const noChat = () => {
  ElNotification.info("该功能暂未实现！");
};
</script>

<style scoped lang="scss">
.user-item {
  --image-content-height: 21px;
  --user-item-padding-left: 12px;
  --user-item-gap: 8px;
  display: flex;
  width: 100%;
  cursor: pointer;
  padding: 8px var(--user-item-padding-left);
  gap: var(--user-item-gap);
  box-sizing: border-box;
  background-color: #e5e4e49e;

  &.active {
    background-color: #c4c4c470;
  }
  &:hover {
    background-color: #d8d7d7;
  }

  .item-left {
    display: flex;
    align-items: center;

    :deep(.el-badge) {
      display: flex;
    }
  }

  .item-right {
    flex: 1;
    display: flex;
    justify-content: space-around;
    overflow: hidden;
    flex-direction: column;

    .right-top {
      display: flex;
      align-items: center;
      font-size: 16px;
      color: #000;

      .top-name {
        width: 100%;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
      }

      .top-time {
        font-size: 14px;
        color: #999999;
      }
    }

    .right-bottom {
      font-size: 14px;
      color: #999999;
      overflow: hidden; /* 确保超出容器的文本被隐藏 */
      white-space: nowrap; /* 确保文本在一行内显示 */
      text-overflow: ellipsis; /* 使用省略号表示被截断的文本 */

      .image-content {
        height: var(--image-content-height);
        border-radius: 0px;
      }
    }
  }
}
</style>
